package lingnan.cn.edu.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import lingnan.cn.edu.User.Orderinf;
import lingnan.cn.edu.util.DbAccess;

public class OrderAndInfService {
	
	public int addOrder(int TotalPrice) {
		Connection conn = DbAccess.getConnection();
		Connection conn1 = DbAccess.getConnection();
		String SQL = "insert into record values(null,?,?)";
		String SQL1 = "select Max(orderId) from record";
		
		Date date = new Date();
		System.out.println(date);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		String PayTime = sdf.format(date);
		System.out.println(PayTime);
		PreparedStatement prep = null;
		PreparedStatement prep1 = null;
		ResultSet rs=null;
		int id = 0;
		try {
			
		    prep = conn.prepareStatement(SQL);
		    prep.setInt(1,TotalPrice);
		    prep.setString(2,PayTime);
			prep.executeUpdate();
			
			prep1 = conn.prepareStatement(SQL1);
			
			rs = prep1.executeQuery();
			if(rs.next()) {
		    id = rs.getInt("Max(orderId)");
		    System.out.println(id+"db");
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally {
			DbAccess.release(conn, prep, rs);
		}
		
		return id;
	}
	
	public void addOrderInf(Orderinf orderInf) {
		Connection conn = DbAccess.getConnection();
		String SQL = "insert into orderInf values(?,?,?,?,?)";
		
		PreparedStatement prep = null;
		ResultSet rs=null;
		try {
			prep = conn.prepareStatement(SQL);
			prep.setInt(1, orderInf.getOrderId());
			prep.setString(2, orderInf.getName());
			prep.setInt(3, orderInf.getPrice());
			prep.setInt(4, orderInf.getCount());
			prep.setInt(5, orderInf.getItemPrice());
			prep.executeUpdate();
		
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally {
			DbAccess.release(conn, prep, rs);
		}
		
	}
}


